package com.frcdi.qms.service.impl;

import com.frcdi.qms.common.constant.UserConstants;
import com.frcdi.qms.common.utils.SecurityUtils;
import com.frcdi.qms.equipment.domain.EquipmentBase;
import com.frcdi.qms.equipment.domain.EquipmentValidation;
import com.frcdi.qms.equipment.service.IEquipmentBaseService;
import com.frcdi.qms.equipment.service.IEquipmentValidationService;
import com.frcdi.qms.masterdata.domain.Change;
import com.frcdi.qms.masterdata.domain.Material;
import com.frcdi.qms.masterdata.domain.Method;
import com.frcdi.qms.masterdata.service.IChangeService;
import com.frcdi.qms.masterdata.service.IMaterialService;
import com.frcdi.qms.masterdata.service.IMethodService;
import com.frcdi.qms.service.SettingService;
import com.frcdi.qms.service.WorkspaceProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Map;

import static com.frcdi.qms.common.CommonConstants.PROCESS_DATA;
import static com.frcdi.qms.common.CommonConstants.PROCESS_TYPE;

/**
 * @author : willian fu
 * @version : 1.0
 */
@Service
public class WorkspaceProcessServiceImpl implements WorkspaceProcessService {

    @Autowired
    private SettingService settingService;
    @Autowired
    private IChangeService changeService;
    @Autowired
    private IEquipmentBaseService equipmentService;
    @Autowired
    private IMethodService methodService;
    @Autowired
    private IMaterialService materialService;
    @Autowired
    private IEquipmentValidationService validationService;

    /**
     * 获取用户可见表单
     *
     * @return
     */
    @Override
    public Object getFormGroups(String name) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder
            .getRequestAttributes();
//        String token = CookieUtil.getCookieValue(servletRequestAttributes.getRequest(), "token");
        return null;
    }

    @Override
    public void updataBusiness(Map formValue, String operation) {
        String processType = String.valueOf(formValue.get(PROCESS_TYPE));
        String processData = String.valueOf(formValue.get(PROCESS_DATA));
        switch (processType) {
            case UserConstants.PROCESS_MATERIAL:
                updataMaterial(processData,operation);
                break;
            case UserConstants.PROCESS_METHOD:
                updataMethod(processData,operation);
                break;
            case UserConstants.PROCESS_EQUIPMENT:
                updataEquipment(processData,operation);
                break;
            case UserConstants.PROCESS_CHANGE:
                updataBusinessChange(processData,operation);
                break;
            case UserConstants.PROCESS_EQUIPMENT_VALIDATION:
                updataValidation(processData,operation);
                break;
        }
    }

    /**
     * 变更申请单
     */
    private void updataBusinessChange(String changNo,String operation) {
        // 修改变更申请单状态
        Change change = new Change();
        change.setChangeId(Long.valueOf(changNo));
        if ("refuse".equals(operation)) {
            change.setAuditResult(UserConstants.AUDIT_FAILED);
            change.setTaskStatus(UserConstants.TASK_NOT);
        }else {
            change.setTaskStatus(UserConstants.TASK_NOT);
        }
        change.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        changeService.updateChange(change);
    }

    /**
     * 设备验证
     */
    private void updataValidation(String validationId,String operation) {
        // 修改验证单据状态
        EquipmentValidation equipmentValidation = new EquipmentValidation();
        equipmentValidation.setValidationId(Long.valueOf(validationId));
        if ("refuse".equals(operation)) {
            equipmentValidation.setAuditResult(UserConstants.AUDIT_FAILED);
            equipmentValidation.setTaskStatus(UserConstants.TASK_NOT);
        }else {
            equipmentValidation.setTaskStatus(UserConstants.TASK_NOT);
        }
        equipmentValidation.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        validationService.updateEquipmentValidation(equipmentValidation);
    }

    /**
     * 设备
     */
    private void updataEquipment(String equipmentId,String operation) {
        EquipmentBase equipment = new EquipmentBase();
        equipment.setEquipmentId(Long.valueOf(equipmentId));
        if ("refuse".equals(operation)) {
            equipment.setAuditResult(UserConstants.AUDIT_FAILED);
            equipment.setTaskStatus(UserConstants.TASK_NOT);
        }else {
            equipment.setTaskStatus(UserConstants.TASK_NOT);
        }
        equipment.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        equipmentService.updateEquipmentBase(equipment);
    }

    /**
     * 方法
     */
    private void updataMethod(String methodId,String operation) {
        Method method = new Method();
        method.setMethodId(Long.valueOf(methodId));
        if ("refuse".equals(operation)) {
            method.setAuditResult(UserConstants.AUDIT_FAILED);
            method.setTaskStatus(UserConstants.TASK_NOT);
        }else {
            method.setTaskStatus(UserConstants.TASK_NOT);
        }
        method.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        methodService.updateMethod(method);

    }

    /**
     * 物料
     */
    private void updataMaterial(String materialId,String operation) {
        Material material = new Material();
        material.setId(Long.valueOf(materialId));
        if ("refuse".equals(operation)) {
            material.setAuditResult(UserConstants.AUDIT_FAILED);
            material.setTaskStatus(UserConstants.TASK_NOT);
        }else {
            material.setTaskStatus(UserConstants.TASK_NOT);
        }
        material.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        materialService.updateMaterial(material);
    }
}
